package tribe.vilt.lost.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;

import java.io.IOException;

/**
 * Created by Tobias Ericsson.
 */
public class PlayAudioActivity extends Activity {

    private static final String TAG = PlayAudioActivity.class.getSimpleName();
    private MediaPlayer player;
    private int oldRingerMode, oldVolume;
    private PowerManager.WakeLock wakeLock;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        TextView textView = new TextView(this);
        textView.setPadding(10, 10, 10, 10);
        String message = getIntent().getStringExtra("message");
        if (message != null && message.length() > 0) {
            textView.setText(message);
        } else {
            textView.setText("Lost?");
        }
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(0xBB660022);
        textView.setTextSize(54);
        textView.setBackgroundColor(0xAA660000);
        setContentView(textView);

        ContentValues values = new ContentValues();
        values.put("address", "alocator");
        values.put("body", message);
        getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
        wakeLock.acquire();

        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        oldRingerMode = audioManager.getRingerMode();
        oldVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_VIBRATE);
        Uri ringToneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
        ringToneUri.getLastPathSegment();

        player = new MediaPlayer();
        try {
            player.setDataSource(this, ringToneUri);
            player.prepare();
        } catch (IOException e) {
            Log.d(TAG, "setup of musicplayer failed " + e.getMessage() + "finish activity");
            finish();
        }

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                Log.d(TAG, "onCompletion finish activity");
                finish();
            }
        });
        player.start();

    }

    public void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();
        if (player != null && !player.isPlaying()) {
            player.start();
        }
    }

    public void onPause() {
        Log.d(TAG, "onPause");
        super.onPause();
    }

    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onPause();
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(oldRingerMode);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, oldVolume, AudioManager.FLAG_VIBRATE);
        if (player != null) {
            player.stop();
            player.reset();
            player.release();
            player = null;
        }

        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
    }
}
